//
//  BHAlertUtil.m
//  Behing
//
//  Created by Lj on 2017/10/9.
//  Copyright © 2017年 lj. All rights reserved.
//

#import "BHAlertUtil.h"

/** 默认简短提示语显示的时间 */
static CGFloat const   kShowTime  = 1.0f;
/* 默认超时时间，30s后自动去除提示框 */
static NSTimeInterval const interval = 30.0f;

static BHAlertUtil *_alertUtil;

@interface BHAlertUtil () {
    MBProgressHUD *loadingHUD;
    MBProgressHUD *promptHUD;
}

@end

@implementation BHAlertUtil

+ (id)alertManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_alertUtil == nil) {
            _alertUtil = [[BHAlertUtil alloc]init];
        }
    });
    return _alertUtil;
}

- (void)showPromptInfo:(NSString *)info {
    [self showPromptInfo:info inView:nil];
}

- (void)showLoading:(NSString *)message {
    [self showLoadingMessage:message inView:nil];
}

- (UIView *)topView {
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    return window.subviews[0];
}

- (void)showLoadingView:(UIView *)view {
    [self showLoadingMessage:nil inView:view];
}

#pragma mark - 简短提示语
- (void)showPromptInfo:(NSString *)message inView:(UIView *)view {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (view) {
            promptHUD = nil;
            promptHUD =  [MBProgressHUD showHUDAddedTo:view animated:YES];
        }else {
            if (promptHUD == nil) {
                promptHUD =  [MBProgressHUD showHUDAddedTo:(view == nil ? [self topView] : view) animated:YES];
            }
        }
        promptHUD.label.text = message;
        promptHUD.label.numberOfLines = 0;
        promptHUD.animationType = MBProgressHUDAnimationZoom;
        promptHUD.mode = MBProgressHUDModeText;
        promptHUD.removeFromSuperViewOnHide = YES;
        [promptHUD hideAnimated:YES afterDelay:kShowTime];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kShowTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            promptHUD = nil;
        });
        
    });
}

#pragma mark - 网络加载提示用
- (void)showLoadingMessage:(NSString *)message inView:(UIView *)view {
    //    dispatch_async(dispatch_get_main_queue(), ^{
    if (loadingHUD == nil) {
        loadingHUD = [[MBProgressHUD alloc] initWithView:(view == nil ? [self topView] : view)];
        loadingHUD.removeFromSuperViewOnHide = YES;
    }
    if (message) loadingHUD.label.text = message;
    else loadingHUD.label.text = nil;
//    loadingHUD.userInteractionEnabled = NO;
//    [[UIApplication sharedApplication].delegate.window addSubview:loadingHUD];
//    [[UIApplication sharedApplication].delegate.window bringSubviewToFront:loadingHUD];
    if (view) {
        [view addSubview:loadingHUD];
        [view bringSubviewToFront:loadingHUD];
    }else {
        [[UIApplication sharedApplication].delegate.window addSubview:loadingHUD];
        [[UIApplication sharedApplication].delegate.window bringSubviewToFront:loadingHUD];
    }
    
    [loadingHUD showAnimated:YES];
//    [self hideAlertDelay];
    //    });
}

#pragma mark - 隐藏提示框
- (void)hiddenLoading {
    [loadingHUD hideAnimated:YES];
    loadingHUD = nil;
}


#pragma mark - 获取view上的hud
- (MBProgressHUD *)hudForView:(UIView *)view {
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:[MBProgressHUD class]]) {
            return (MBProgressHUD *)subview;
        }
    }
    return nil;
}

#pragma mark - 超时后（默认30s）自动隐藏加载提示
- (void)hideAlertDelay {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hiddenLoading];
    });
}

@end
